package net.ssta.util.bill.chargeobj;

import java.sql.Connection;
import java.sql.SQLException;

import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.Record;
import net.ssta.util.bill.UserBill;
import net.ssta.util.bill.UserBillException;
import net.ssta.util.bill.UserPoint;

import org.apache.log4j.Logger;

public class CmsCharge  implements ChargeInterface{
	private static Logger log = Logger.getLogger(CmsCharge.class.getClass()); 
	private String channelId;
	private String infoId;
	private String title;
	public CmsCharge(String _channelId,String _infoId,String _title){
		this.channelId=_channelId;
		this.infoId=_infoId;
		this.title=_title;
	}
	/**
	 * 根据扣费对象的主键，来判断其所需的费用
	 * @param chargeId
	 * @return
	 * @throws UserBillException 
	 */
	public int calCostNumber() throws UserBillException{
		Connection conn = null;
    	try {
    		conn = DBConnectionProvider.getConnection(UserPoint.jndiName);
	    	Record r=new Record();
	    	r.setConnection(conn);
	    	r.setEntityName("V_CMS_CHANNEL");
	    	r.setPrimaryKey("CHANNEL_ID");
	    	r.addData("CHANNEL_ID",channelId);
	    	r=r.findByPrimaryKey();
	    	return r.getInt("BILL");
		} catch (SQLException e) {
			log.error("根据频道ID查找频道分数出错："+e); 
			throw new UserBillException(UserBillException.calCostNumError,e.getMessage());
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	public String getChargeType(){
		return "cms";
	}
	
	public UserBill initBill(UserBill bill){
		bill.setCType(this.getChargeType());
		bill.setChannelId(channelId);
		bill.setInfoId(infoId);
		bill.setTitle(title);
		return bill;
	}
	
}
